package crud;

import java.util.List;

import javax.persistence.EntityManager;

import entities.Employee;
import entities.User;

public class UserCRUD extends AbstractGRUD<User> {
	public UserCRUD() {
		super(User.class);
	}
	
	// Xóa user và set null cột idUser cho employee.
	public String delete(String idUser) {		
		EntityManager em = this.getEm();
		String result = "success";
		try {
			em.getTransaction().begin();
			User user = this.readById(idUser);
			List<Employee> _Employees = user.getEmployees();
			for (Employee employee : _Employees) {
				employee.setUser(null);
				em.merge(employee);
			}
			em.remove(user);
			em.getTransaction().commit();

		} catch (Exception e) {
			result = e.getMessage();
			em.getTransaction().rollback();
		}
		return result;
	}

}
